<?php

/**
 * This is the model class for table "{{item}}".
 *
 * The followings are the available columns in table '{{item}}':
 * @property integer $id
 * @property integer $category_id
 * @property string $title
 * @property integer $item_condition_id
 * @property integer $sell_type
 * @property string $start_price
 * @property string $buy_it_now_price
 * @property integer $duration
 * @property string $description
 * @property integer $quantity
 * @property integer $owner_id
 * @property string $created_time
 * @property string $updated_time
 * @property integer $item_state
 */
class Item extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Item the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{item}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('category_id, title, item_condition_id, sell_type, start_price, buy_it_now_price, duration, description, quantity, owner_id, created_time, item_state', 'required'),
			array('category_id, item_condition_id, sell_type, duration, quantity, owner_id, item_state', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>50),
			array('start_price, buy_it_now_price', 'length', 'max'=>10),
			array('description', 'length', 'max'=>2000),
			array('updated_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, category_id, title, item_condition_id, sell_type, start_price, buy_it_now_price, duration, description, quantity, owner_id, created_time, updated_time, item_state', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'category_id' => 'Category',
			'title' => 'Title',
			'item_condition_id' => 'Item Condition',
			'sell_type' => 'Sell Type',
			'start_price' => 'Start Price',
			'buy_it_now_price' => 'Buy It Now Price',
			'duration' => 'Duration',
			'description' => 'Description',
			'quantity' => 'Quantity',
			'owner_id' => 'Owner',
			'created_time' => 'Created Time',
			'updated_time' => 'Updated Time',
			'item_state' => 'Item State',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('category_id',$this->category_id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('item_condition_id',$this->item_condition_id);
		$criteria->compare('sell_type',$this->sell_type);
		$criteria->compare('start_price',$this->start_price,true);
		$criteria->compare('buy_it_now_price',$this->buy_it_now_price,true);
		$criteria->compare('duration',$this->duration);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('quantity',$this->quantity);
		$criteria->compare('owner_id',$this->owner_id);
		$criteria->compare('created_time',$this->created_time,true);
		$criteria->compare('updated_time',$this->updated_time,true);
		$criteria->compare('item_state',$this->item_state);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public function getCategories()
	{
		$models = Category::model()->findAll();
		return CHtml::listData($models, 'id', 'name');
	}
	
	public function getItemConditions()
	{
		$models = Thesaurus::model()->findAll('type_id = :ID',array(':ID' => 1));
		return CHtml::listData($models, 'id', 'text_value');
	}
	
	public function getSellTypes()
	{
		$models = Thesaurus::model()->findAll('type_id = :ID',array(':ID' => 2));
		return CHtml::listData($models, 'id', 'text_value');
	}
}